home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 25 / AMIGAplus Sonderheft 25 (2000)(Falke)(DE)(Track 1 of 4)[!].iso / Updates / HD-Installer / jst_dev / sources / src / DiskTools / ripcorefiles.c < prev    next >
C/C++ Source or Header  |  2000-04-12  |  5KB  |  222 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4.  
  5. #include <dos/dos.h>
  6. #include <proto/dos.h>
  7. #include <proto/exec.h>
  8. #include <exec/execbase.h>
  9. #include <exec/libraries.h>
  10. #include <exec/memory.h>
  11. #include <pragmas/dos_pragmas.h>
  12. #include <string.h>
  13.  
  14. #define RAWTRACK_LEN 0x8000
  15. #define TRACK_LEN 0x1600
  16. #define START_TRACK 1
  17. #define END_TRACK 159
  18. #define NB_TRACKS END_TRACK-START_TRACK+1
  19. #define NB_RETRIES 10
  20.  
  21. extern struct ExecBase *SysBase;
  22.  
  23. extern APTR InitTrackDisk(ULONG);
  24. extern void ShutTrackDisk(void);
  25. extern ULONG CheckDiskIn (void);
  26.  
  27. extern ULONG ReadRawTrackIndex(ULONG,UBYTE *);
  28. extern ULONG DecodeTrack(UBYTE *, UBYTE *);
  29. extern ULONG WriteFiles(UBYTE *);
  30.  
  31. UBYTE *rawTrackBuffer=0L;
  32. UBYTE *decTrackBuffer=0L;
  33. char *charunit="DF0:";
  34. int allocated=0,diskinit=0;
  35. BPTR oldlock,newlock;
  36.  
  37.  
  38. void CloseAll(char *mess)
  39.  
  40. {
  41. if (mess) printf("** %s\n",mess);
  42.  
  43. if (diskinit) ShutTrackDisk();
  44. if (allocated) Inhibit(charunit,FALSE);
  45. if (rawTrackBuffer) FreeMem((APTR)rawTrackBuffer,RAWTRACK_LEN);
  46. if (decTrackBuffer) FreeMem((APTR)decTrackBuffer,TRACK_LEN*NB_TRACKS);
  47.  
  48. if (newlock)
  49.   {
  50.   CurrentDir(oldlock);
  51.   UnLock(newlock);
  52.   }
  53.  
  54. exit(0);
  55. }
  56.  
  57. void BreakHandle(int code)
  58.  
  59. {
  60. CloseAll("User break");
  61. }
  62.  
  63.  
  64. main(unsigned int argc,char ** argv)
  65.  
  66.  
  67. {
  68.   ULONG diskunit,numid,readid;
  69.   char strid[10];
  70.   int i,readok,retries,dcrslt;
  71.   char *trkptr;
  72.  
  73.   printf("\2331;37;40mCore Design Protected Format file ripper\2330;31;40m - by JOTD 1997\n");
  74.  
  75.   switch(argc)
  76.    {
  77.    case 0:
  78.    case 1:
  79.    case 2:
  80.     printf("Usage : ripcorefiles <unit number> <destination-dir> [<diskid>]\n");
  81.     if (!argc) Delay(200);
  82.     exit(0);
  83.  
  84.    default:
  85.     /* Try to lock the specified directory */
  86.  
  87.     newlock = Lock(argv[2],ACCESS_READ);
  88.     if (!newlock) CloseAll("Couldn't lock destination directory");
  89.  
  90.     /* Save current directory lock */
  91.     /* And change directory */
  92.  
  93.     oldlock = CurrentDir(newlock);
  94.     break;
  95.    }
  96.  
  97.   signal(SIGINT,BreakHandle);
  98.   
  99.  
  100.   diskunit=(ULONG)argv[1][0]-'0';
  101.  
  102.   if ((diskunit>3)||(diskunit<0)) {printf("** Unit %d unavailable\n",diskunit);CloseAll(0);}
  103.  
  104.   charunit[2]=diskunit+'0';
  105.  
  106.   if (SysBase->LibNode.lib_Version>36)
  107.     {
  108.     if(Inhibit(charunit,DOSTRUE)==FALSE) CloseAll("Can't allocate device !");
  109.     allocated=1;
  110.     }
  111.  
  112.   if (argc<4) numid=0; /* no ID check */
  113.  
  114.   else
  115.      {
  116.      strncpy(strid,argv[3],8);
  117.      switch(strid[0])
  118.     {
  119.     case '\'':
  120.         numid=(((int)(strid[1]))<<24)+(((int)(strid[2]))<<16)+
  121.               (((int)(strid[3]))<<8)+((int)(strid[4]));
  122.         break;
  123.  
  124.     default: sscanf(strid,"%x",&numid);break;
  125.  
  126.     }
  127.      }
  128.  
  129.   if ((ULONG)InitTrackDisk(diskunit)<0) {printf("** Can't open unit %d !",diskunit);CloseAll(0);}
  130.   diskinit=1;
  131.   if (CheckDiskIn()) {printf("** No disk in unit %d !\n",diskunit);CloseAll(NULL);}
  132.  
  133.   decTrackBuffer=(char *)AllocMem(TRACK_LEN*NB_TRACKS,MEMF_CLEAR);
  134.   if (decTrackBuffer==0L) CloseAll("Can't allocate memory.");
  135.  
  136.   rawTrackBuffer=(char *)AllocMem(RAWTRACK_LEN,MEMF_CHIP|MEMF_CLEAR);
  137.   if (rawTrackBuffer==0L) CloseAll("Can't allocate chip memory.");
  138.  
  139.   trkptr=decTrackBuffer;
  140.  
  141.   printf("Reading disk data...\n");
  142.  
  143.   for (i=START_TRACK;i<END_TRACK+1;i++)
  144.     {
  145.     retries=0;
  146.     readok=0;
  147.  
  148.         do
  149.         {
  150.              if (ReadRawTrackIndex(i,rawTrackBuffer))
  151.             {
  152.                 if (retries>NB_RETRIES) CloseAll("Low level disk read error!");
  153.             }
  154.  
  155.              else
  156.              {
  157.                  dcrslt=DecodeTrack(rawTrackBuffer,trkptr);
  158.  
  159.                  if (!dcrslt)
  160.                     {
  161.                         if (i==2)
  162.                         {
  163.                             /* disk ID check if specified */
  164.  
  165.                             readid=(((int)(trkptr[0x10]))<<24)+(((int)(trkptr[1+0x10]))<<16)+
  166.                                   (((int)(trkptr[2+0x10]))<<8)+((int)(trkptr[3+0x10]));
  167.  
  168.                             if (numid)
  169.                                 {
  170.                                  if (readid!=numid) 
  171.                                     CloseAll("Disk ID does not match");
  172.                                 }
  173.                             else
  174.                                 {
  175.                                  printf("DiskID : %X\n",readid);
  176.                                 }
  177.                         }
  178.  
  179.                         readok=1;
  180.                         trkptr+=TRACK_LEN;
  181.                     }
  182.                 else
  183.                     {
  184.                         if (retries>NB_RETRIES)
  185.                         {
  186.                             switch(dcrslt)
  187.                             {    
  188.                             case 1: printf("** Decode error Track %d\n",i);/*CloseAll("Decode disk read error!");*/
  189.                                     readok=1;
  190.                                     trkptr+=TRACK_LEN;
  191.                                     break;
  192.  
  193.                             default: printf("** Error Track %d\n",i);CloseAll("No sync found!");
  194.                             }
  195.                         }
  196.                         else
  197.                         {
  198.                             Delay(10);
  199.                         }
  200.                     }
  201.  
  202.              }
  203.  
  204.     retries++;
  205.     }
  206.     while (!readok);
  207.  
  208.  
  209.     }
  210.  
  211.     switch(WriteFiles(decTrackBuffer))
  212.  
  213.     {
  214.     case 0 : printf("Disk read successfuly!\n");CloseAll(NULL);
  215.     case -1: CloseAll("File Creation Error");
  216.     case -3: CloseAll("Directory Structure Error");
  217.     case -5: CloseAll("Memory Allocation Error");
  218.     default: CloseAll("Unknown error");
  219.     }
  220.  
  221. }
  222.